package com.spark.lola.service.gds;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.spark.lola.entity.gds.CategoryInfo;
import com.spark.lola.entity.gds.ProductInfo;
import com.spark.lola.feign.GdsFeignService;
import com.spark.lola.feign.LogisticsFeignService;
import com.spark.lola.mapper.pur.OrderInfoMapper;
import com.spark.lola.service.gds.ICategoryInfoService;
import com.spark.lola.service.gds.IProductInfoService;
import com.spark.lola.support.common.constant.CommonConstant;
import com.spark.lola.vo.ProductVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: DengYeJun
 * @Date: 2020/4/27 13:40
 */
public class GdsFeignServiceImpl implements GdsFeignService {
    private ICategoryInfoService categoryInfoService;
    private IProductInfoService productInfoService;
    private LogisticsFeignService logisticsFeignService;

    public GdsFeignServiceImpl(ICategoryInfoService categoryInfoService, IProductInfoService productInfoService,
                               LogisticsFeignService logisticsFeignService) {
        this.categoryInfoService = categoryInfoService;
        this.productInfoService = productInfoService;
        this.logisticsFeignService = logisticsFeignService;
    }

    @Override
    public List<CategoryInfo> findCategory() {
        QueryWrapper<CategoryInfo> query = new QueryWrapper<>();
        query.eq("used", CommonConstant.Y);
        return categoryInfoService.list(query);
    }

    @Override
    public List<ProductInfo> findProduct(Long categoryId, String key) {
        QueryWrapper<ProductInfo> query = new QueryWrapper<>();
        query.select("id,name");
        query.eq("used", CommonConstant.Y);
        if (categoryId.intValue() > 0) {
            query.eq("categoryId", categoryId);
        }
        if (!StringUtils.isEmpty(key)) {
            query.and(o -> o.like("no", key).or().like("name", key));
        }
        return productInfoService.list(query);
    }

    @Override
    public List<ProductVo> findProductVo() {
        QueryWrapper<ProductInfo> query = new QueryWrapper<>();
        query.select("id,no,name,picture");
        query.eq("used", CommonConstant.Y);
        query.eq("category_id",2L);
        return productInfoService.list(query).stream().map(item ->
                new ProductVo(item.getId(), item.getNo(), item.getName(), item.getPicture())).collect(Collectors.toList());
    }

    @Override
    public Map<String, BigDecimal> statistics(Long productId, String start, String end) {
        Map<String, BigDecimal> map = productInfoService.supStatistics(productId, start, end);
        // TODO 获取周期内货品物流重量
        BigDecimal transportWeight = logisticsFeignService.getLogisticsTotalWeight(start, end, productId);
        map.put("transportWeight", transportWeight);
        return map;
    }

}
